/**
 * @author Johann Prieur
 */
package mygui;

import javax.swing.*;
import javax.swing.border.*;
import javax.swing.tree.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;

import kernel.*;
import utils.*;

public class PreferencesProperties extends Properties {

    private MonIHM _gui;
    //private Simulator _kernel;
    private DataSimulator _data;

    private JTabbedPane _tabs;
    private GeneralPanel _general;
    private DisplayPropertiesPanel _display;

    public PreferencesProperties(MonIHM gui)
    {

	_gui = gui;
	//_kernel = Simulator.getSimulator();
	_data = (DataSimulator)gui.getSimulator().getProperties();

	_general = new GeneralPanel();
	_display = new DisplayPropertiesPanel();

	setTitle("Preferences");
	_tabs = new JTabbedPane(JTabbedPane.TOP,
 				JTabbedPane.SCROLL_TAB_LAYOUT);

	add("North", new ObjectIdCard(Type.properties,
				      _data.getName(),
				      "Preferences"));

	_tabs.add(_general, "Simulation");
	_tabs.add(_display, "Display");

	add("Center", _tabs);
	pack();
	WindowUtilities.centerFrame(this);
	setVisible(true);
    }
    
    public void apply()
    {
	_general.apply();
	_display.apply();
	Simulator.getSimulator().setProperties(_data);
    }

    private class GeneralPanel extends JPanel implements ActionListener
    {
	private JTextField _name;
 	private String _name_tip = "Name of the simulation";
	
	private JTextField _duration;
	private String _duration_tip = "Duration of the simulation";
	
	private JComboBox _routage;

	private JTextField _resultFileDir;
	private String _resultFileDir_tip = "Directory where result files will be put";

	private JButton _browse;

	private GeneralPanel()
	{
	    setLayout(new BorderLayout());
	    JPanel _grid = new JPanel();
	    add(_grid,"North");

	    _grid.setLayout(new GridLayout(6,2,4,4));
	    _grid.setBorder(new EmptyBorder(4,4,4,4));

	    _name = new JTextField(_data.getName());
	    _duration = new JTextField(Double.toString(_data.getDuration()));

	    _grid.add(new JLabel("Name"));
	    _grid.add(_name);
	    _name.setToolTipText(_name_tip);

	    JLabel mode = new JLabel();
	    switch(Simulator.getSimulator().getMode()){
	    case 0:
		mode = new JLabel("Infrastructure");break;
	    case 1:
		mode = new JLabel("Adhoc");break;
	    case 2:
		mode = new JLabel("Hybrid");break;
	    case 3:
		mode = new JLabel("Sensor networks");break;
	    }
	    _grid.add(new JLabel("Mode"));
	    _grid.add(mode);

	    _grid.add(new JLabel("Duration"));
	    _grid.add(_duration);

	    if(Simulator.getSimulator().getMode() == Simulator.SENSOR){
		_grid.add(new JLabel("Routage algorithm"));
		
		DataSensorSimulator data = (DataSensorSimulator)_data;
	    
		StrategyChoser sc = new StrategyChoser(DataSensorSimulator.ROUTING_PACK);
		sc.checkSuperclass(DataSensorSimulator.ROUTING_INTER);
		Vector<Class> strats = sc.getStrategies();
		Vector<String> strat_labels = new Vector();
		for(Class strat : strats){
		    String[] path = strat.toString().split("[.]");
		    strat_labels.add(path[path.length-1]);
		}
		_routage = new JComboBox(strat_labels);
		Class routage = data.getRouting().getClass();
		String[] path2 = routage.toString().split("[.]");
		_routage.setSelectedItem(path2[path2.length-1]);

		_grid.add(_routage);
	    }

	    add(new JLabel("Result directory"),"Center");

	    
	    JPanel _bottom = new JPanel();
	    _bottom.setLayout(new GridLayout(1,2,4,4));
	    _bottom.setBorder(new EmptyBorder(4,4,4,4));

	    _resultFileDir = new JTextField(_data.getResultDir());
	    _resultFileDir.setToolTipText(_resultFileDir_tip);
	    _browse = new JButton("Browse");
	    _browse.addActionListener(this);

	    _bottom.add(_resultFileDir);
	    _bottom.add(_browse);

	    add(_bottom,"South");
	}

	public void apply()
	{
	    _data.setName(_name.getText());
	    _data.setResultDir(_resultFileDir.getText());
	    _data.setDuration(Double.parseDouble(_duration.getText()));
	    if(Simulator.getSimulator().getMode() == Simulator.SENSOR){
		DataSensorSimulator data = (DataSensorSimulator)_data;
		data.setRouting((String)_routage.getSelectedItem());
	    }
	}

	public void actionPerformed(ActionEvent e){
	    Component source = (Component)(e.getSource());
	    if(source == _browse){
		JFileChooser chooser = new JFileChooser(_resultFileDir.getText());
		chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
		int returnVal = chooser.showOpenDialog(this);
		if (returnVal == JFileChooser.APPROVE_OPTION) {
		    _resultFileDir.setText(chooser.getSelectedFile().toString());
		}
	    }
	}
    }

    private class DisplayPropertiesPanel extends JPanel 
    {
	private JCheckBox _fill_obstacles;
	private JCheckBox _draw_reach_radius;
	private JCheckBox _draw_measure_radius;
	private JCheckBox _draw_names;
	private JCheckBox _draw_grid;
	private JCheckBox _draw_sensor_names;
	
	private DisplayPropertiesPanel()
	{
	    setLayout(new GridLayout(5,1,4,4));
	    setBorder(new EmptyBorder(4,4,4,4));

	    _fill_obstacles = new JCheckBox("Fill obstacles",
					    _gui._display.fill_obstacles);
	    _draw_reach_radius = new JCheckBox("Draw reach radius",
					       _gui._display.draw_reach_radius);
	    _draw_measure_radius = new JCheckBox("Draw measure radius",
						 _gui._display.draw_measure_radius);
	    _draw_names = new JCheckBox("Draw objects names",
					_gui._display.draw_names);
	    _draw_grid = new JCheckBox("Draw the grid",
				       _gui._display.draw_grid);
	    _draw_sensor_names = new JCheckBox("Draw sensors' names",
				       _gui._display.draw_sensor_names);

	    add(_draw_names);
	    add(_draw_grid);
	    add(_fill_obstacles);
	    add(_draw_reach_radius);
	    if(Simulator.getSimulator().getMode() == Simulator.SENSOR) {
		add(_draw_measure_radius);
		add(_draw_sensor_names);
	    }
	    
	}

	public void apply()
	{
	    _gui._display.fill_obstacles = _fill_obstacles.isSelected();
	    _gui._display.draw_reach_radius = _draw_reach_radius.isSelected();
	    _gui._display.draw_measure_radius = _draw_measure_radius.isSelected();
	    _gui._display.draw_names = _draw_names.isSelected();
	    _gui._display.draw_grid = _draw_grid.isSelected();
	    _gui._display.draw_sensor_names = _draw_sensor_names.isSelected();
	}
    }
    
}